﻿using System;
using System.Data.SqlClient;
using System.Security.Cryptography;
using System.Web.Security;

namespace TX
{
    public static class SaltedHashing
    {
        private static string ConnString = Properties.Settings.Default.Karbel4ConnectionString;
        private const int SaltByteSize = 10;    //16 idi!

        public static string[] Hash(string username, string password)
        {
            using (SqlConnection con = new SqlConnection(ConnString))
            {
                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                byte[] buff = new byte[SaltByteSize];
                rng.GetBytes(buff);
                string salt = Convert.ToBase64String(buff).Substring(0, 10);
                return new string[]{salt, FormsAuthentication.HashPasswordForStoringInConfigFile(salt + password, "SHA1")};

                //new SqlCommandBuilder(da);
                //da.Update(ds.Tables["Contact"]);
            }
        }

    }
}